package com.zzyl.serve.service.impl;

import java.util.List;
import java.util.stream.Collectors;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zzyl.common.exception.ServiceException;
import com.zzyl.common.exception.base.BaseException;
import com.zzyl.serve.domain.NursingLevel;
import com.zzyl.serve.domain.NursingProjectPlan;
import com.zzyl.serve.dto.NursingPlanDto;
import com.zzyl.serve.mapper.NursingLevelMapper;
import com.zzyl.serve.mapper.NursingProjectPlanMapper;
import com.zzyl.serve.vo.NursingPlanVo;
import com.zzyl.serve.vo.NursingProjectPlanVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.stereotype.Service;
import com.zzyl.serve.mapper.NursingPlanMapper;
import com.zzyl.serve.domain.NursingPlan;
import com.zzyl.serve.service.INursingPlanService;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

/**
 * 护理计划Service业务层处理
 *
 * @author qqq
 * @date 2024-12-27
 */
@Service
public class NursingPlanServiceImpl extends ServiceImpl<NursingPlanMapper, NursingPlan> implements INursingPlanService {
    @Autowired
    private NursingPlanMapper nursingPlanMapper;

    @Autowired
    private NursingProjectPlanMapper nursingProjectPlanMapper;

    @Autowired
    private NursingLevelMapper nursingLevelMapper;

    /**
     * 查询护理计划
     *
     * @param id 护理计划主键
     * @return 护理计划
     */
    @Override
    @CacheEvict(cacheNames = "NursingPlan")
    public NursingPlanVo selectNursingPlanById(Integer id) {
        //先查询护理计划
        NursingPlan nursingPlan = getById(id);
        NursingPlanVo vo = new NursingPlanVo();
        BeanUtils.copyProperties(nursingPlan, vo);

        List<NursingProjectPlanVo> voList = nursingProjectPlanMapper.selectByPlanId(id);
        /*//通过护理计划id查询护理项目信息
        LambdaQueryWrapper<NursingProjectPlan> lambdaQueryWrapper =  new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(NursingProjectPlan::getPlanId, id);
        List<NursingProjectPlan> nursingProjectPlans = nursingProjectPlanMapper.selectList(lambdaQueryWrapper);
        // 转成vo
        if(!CollectionUtils.isEmpty(nursingProjectPlans)){
            List<NursingProjectPlanVo> voList = nursingProjectPlans.stream().map(n -> {
                NursingProjectPlanVo v = new NursingProjectPlanVo();
                BeanUtils.copyProperties(n, v);
                return v;
            }).collect(Collectors.toList());
            //设置护理项目信息到护理计划对象里
            vo.setProjectPlans(voList);
        }*/
        vo.setProjectPlans(voList);
        return vo;
    }

    /**
     * 查询护理计划列表
     *
     * @param nursingPlan 护理计划
     * @return 护理计划
     */
    @Override
    public List<NursingPlan> selectNursingPlanList(NursingPlan nursingPlan) {
        return nursingPlanMapper.selectNursingPlanList(nursingPlan);
    }

    /**
     * 新增护理计划
     *
     * @param nursingPlanDto 护理计划
     * @return 结果
     */
    @Override
    @Transactional
    @CacheEvict(cacheNames = "NursingPlan", allEntries = true)
    public int insertNursingPlan(NursingPlanDto nursingPlanDto) {
        //1.保存护理计划
        NursingPlan nursingPlan = new NursingPlan();
        BeanUtils.copyProperties(nursingPlanDto, nursingPlan);
        boolean flag = save(nursingPlan);
        //2.遍历护理项目，设置护理计划的id
        List<NursingProjectPlan> projectPlans = nursingPlanDto.getProjectPlans();
        if (!CollectionUtils.isEmpty(projectPlans)) {
            projectPlans.forEach(pp -> pp.setPlanId(nursingPlan.getId()));
            //3.批量保存护理项目计划关系
            nursingProjectPlanMapper.batchInsert(projectPlans);
        }
        return flag ? 1 : 0;
    }

    /**
     * 修改护理计划
     *
     * @param nursingPlanDto 护理计划
     * @return 结果
     */
    @Transactional
    @Override
    @CacheEvict(cacheNames = "NursingPlan", allEntries = true)
    public int updateNursingPlan(NursingPlanDto nursingPlanDto) {
        // 更新护理计划
        NursingPlan nursingPlan = BeanUtil.toBean(nursingPlanDto, NursingPlan.class);
        boolean flag = updateById(nursingPlan);
        nursingProjectPlanMapper.deleteByPlanId(nursingPlanDto.getId());
        List<NursingProjectPlan> projectPlans = nursingPlanDto.getProjectPlans();
        if(!CollectionUtil.isEmpty(projectPlans)){
            //设置护理计划的id
            projectPlans.forEach(n->n.setPlanId(nursingPlanDto.getId()));
            nursingProjectPlanMapper.batchInsert(projectPlans);
        }
        return flag?1:0;
    }

    /**
     * 批量删除护理计划
     *
     * @param ids 需要删除的护理计划主键
     * @return 结果
     */
    @Override
    @CacheEvict(cacheNames = "NursingPlan", allEntries = true)
    public int deleteNursingPlanByIds(Integer[] ids) {
        //获取要删除的id
        return removeByIds(List.of(ids)) ? 1 : 0;
    }

    /**
     * 删除护理计划信息
     *
     * @param id 护理计划主键
     * @return 结果
     */
    @Transactional
    @Override
    @CacheEvict(cacheNames = "NursingPlan", allEntries = true)
    public int deleteNursingPlanById(Integer id) {
        //判断是否被护理等级使用
        LambdaQueryWrapper<NursingLevel> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(NursingLevel::getLplanId, id);
        Long count = nursingLevelMapper.selectCount(lambdaQueryWrapper);
        if(count > 0){
            throw new ServiceException("该计划被护理等级使用了，不能删除!");
        }
        //删除护理计划与护理项目的关系
        nursingProjectPlanMapper.deleteByPlanId(id);
        //删除护理计划
        return removeById(id) ? 1 : 0;
    }
}
